//
//  UIColor_TTEx.swift
//  TT_Swift
//
//  Created by Lenny on 16/5/11.
//  Copyright © 2016年 Lenny. All rights reserved.
//

import Foundation
import UIKit

extension UIColor {
    
    //16进制的颜色值
    class func colorWithHex(hex:NSString) -> UIColor{
        
        var red:CUnsignedInt = 0, green:CUnsignedInt = 0, blue:CUnsignedInt = 0
        NSScanner(string: hex.substringToIndex(2)).scanHexInt(&red)
        let str:NSString = hex.substringToIndex(4)
        NSScanner(string: str.substringFromIndex(2)).scanHexInt(&green)
        NSScanner(string: hex.substringFromIndex(4)).scanHexInt(&blue)
        return UIColor(red: CGFloat(red) / 255, green: CGFloat(green) / 255, blue: CGFloat(blue) / 255, alpha: CGFloat(1))
    }
    
    //颜色转成图片
    class func colorToImage(color:UIColor) -> UIImage {
        
        let rect:CGRect = CGRectMake(0, 0, 1, 1)
        UIGraphicsBeginImageContext(rect.size)
        let context:CGContextRef = UIGraphicsGetCurrentContext()!
        CGContextSetFillColorWithColor(context, color.CGColor)
        CGContextFillRect(context, rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}